import { DataCenter } from '../../Runtime/DataCenter'
import { _decorator, Component, Sprite, UITransform, Animation } from 'cc'
import { PLAYER_STATE, PlayerStateMachine } from './PlayerStateMachine'
import { Role } from '../../Base/Role'
import { createUINode } from '../../Utils'
import { SmokeController } from './SmokeController'
import { SMOKE_STATE } from './SmokeStateMachine'
const { ccclass, property } = _decorator

@ccclass('PlayerController')
export class PlayerController extends Role {
  //人物当前的位置
  x: number = 1
  y: number = 5
  //人物状态机
  _fsm: PlayerStateMachine = null

  //人物路径烟雾控制器
  smokeController: SmokeController = null

  //初始化脚本
  init() {
    console.log('PlayerController init')
    //创建人物精灵
    this.createRoleSprite()
    this.createSmoke()
    //初始化状态机
    this._fsm = new PlayerStateMachine(this._animationComponent)
    this._fsm.stateName = PLAYER_STATE.IDLE_RIGHT
    this.initEvent()

    //产生烟雾
    this.smokeController.generateSmoke(SMOKE_STATE.IDLE_TOP, 1, 4)
  }
  //初始化事件监听
  initEvent() {}
  //切换状态
  switchState(state: PLAYER_STATE) {
    switch (state) {
      case PLAYER_STATE.IDLE_TOP:
      case PLAYER_STATE.IDLE_LEFT:
      case PLAYER_STATE.IDLE_RIGHT:
      case PLAYER_STATE.IDLE_BOTTOM:
        this.onIdel()
        break
      case PLAYER_STATE.TURNLEFT_TOP:
      case PLAYER_STATE.TURNLEFT_LEFT:
      case PLAYER_STATE.TURNLEFT_RIGHT:
      case PLAYER_STATE.TURNLEFT_BOTTOM:
        this.onTurnLeft()
        break
      case PLAYER_STATE.TURNRIGHT_TOP:
      case PLAYER_STATE.TURNRIGHT_LEFT:
      case PLAYER_STATE.TURNRIGHT_RIGHT:
      case PLAYER_STATE.TURNRIGHT_BOTTOM:
        this.onTurnLeft()
        break
      case PLAYER_STATE.ATTACK_TOP:
      case PLAYER_STATE.ATTACK_LEFT:
      case PLAYER_STATE.ATTACK_RIGHT:
      case PLAYER_STATE.ATTACK_BOTTOM:
        this.onAttack()
        break
      case PLAYER_STATE.DEATH_TOP:
      case PLAYER_STATE.DEATH_LEFT:
      case PLAYER_STATE.DEATH_RIGHT:
      case PLAYER_STATE.DEATH_BOTTOM:
        this.onDead()
      case PLAYER_STATE.MOVE:
        this.onMove()
        break
      default:
        break
    }
  }

  //生成人物移动产生的烟雾
  createSmoke() {
    const smoke = createUINode('Smoke')
    smoke.parent = this.node.parent
    this.smokeController = smoke.addComponent(SmokeController)
    this.smokeController.init()
  }

  //待机状态
  onIdel() {}
  //攻击状态
  onAttack() {}
  //左转状态
  onTurnLeft() {}
  //右转状态
  onTurnRight() {}
  //死亡状态
  onDead() {}
  //移动状态
  onMove() {}
}
